import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import cn.edu.baiyunu.myapplication.ui.theme.BudgetBlue
import cn.edu.baiyunu.myapplication.ui.theme.BudgetLightBlue
import cn.edu.baiyunu.myapplication.ui.theme.PurpleGrey40
import cn.edu.baiyunu.myapplication.ui.theme.Purple80
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.unit.IntSize
import kotlin.math.min

/**
 * 创建一个动态渐变背景
 */
@Composable
fun GradientBackground(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit
) {
    // 记住屏幕尺寸
    var size by remember { mutableStateOf(IntSize.Zero) }
    
    // 创建无限过渡动画
    val infiniteTransition = rememberInfiniteTransition(label = "gradientTransition")
    
    // 动画值1：控制第一个渐变位置
    val offset1 by infiniteTransition.animateFloat(
        initialValue = 0f,
        targetValue = 1f,
        animationSpec = infiniteRepeatable(
            animation = tween(
                durationMillis = 10000,
                delayMillis = 0
            )
        ),
        label = "gradientOffset1"
    )
    
    // 动画值2：控制第二个渐变位置
    val offset2 by infiniteTransition.animateFloat(
        initialValue = 0f,
        targetValue = 1f,
        animationSpec = infiniteRepeatable(
            animation = tween(
                durationMillis = 15000,
                delayMillis = 100
            )
        ),
        label = "gradientOffset2"
    )
    
    // 创建渐变色列表
    val colors1 = listOf(
        BudgetBlue.copy(alpha = 0.6f),
        BudgetLightBlue.copy(alpha = 0.2f),
        Purple80.copy(alpha = 0.6f)
    )
    
    val colors2 = listOf(
        PurpleGrey40.copy(alpha = 0.5f),
        Color.Transparent,
        BudgetBlue.copy(alpha = 0.5f)
    )
    
    Box(
        modifier = modifier
            .fillMaxSize()
            .onSizeChanged { size = it }
            .graphicsLayer {
                // 使用graphicsLayer可以减少重组和重绘
                alpha = 0.99f  // 优化性能，避免不必要的重绘
            }
            .drawWithCache {
                // 使用drawWithCache优化渲染性能
                val width = size.width.toFloat()
                val height = size.height.toFloat()
                val radius = min(width, height) * 1.5f
                
                val brush1 = Brush.radialGradient(
                    colors = colors1,
                    center = Offset(
                        x = width * 0.25f + width * 0.5f * offset1,
                        y = height * (0.5f + 0.3f * offset2)
                    ),
                    radius = radius,
                    tileMode = TileMode.Clamp
                )
                
                val brush2 = Brush.radialGradient(
                    colors = colors2,
                    center = Offset(
                        x = width * (0.7f - 0.4f * offset2),
                        y = height * (0.3f + 0.4f * offset1)
                    ),
                    radius = radius,
                    tileMode = TileMode.Clamp
                )
                
                onDrawBehind {
                    // 绘制第一个渐变
                    drawRect(brush = brush1)
                    // 绘制第二个渐变
                    drawRect(brush = brush2)
                }
            }
    ) {
        content()
    }
} 